package com.first.nell.loan

import android.annotation.SuppressLint
import android.view.View
import androidx.lifecycle.ViewModel
import androidx.lifecycle.ViewModelProvider
import com.first.nell.base.BaseActivity
import com.first.nell.data.ReqBase
import com.first.nell.databinding.ActivityFirstNellLoanConfirmBinding
import com.first.nell.enums.PageTagEnum
import com.first.nell.extend.*
import com.first.nell.launch.UserStateViewModel
import com.first.nell.utils.InfoProcessUtil
import com.first.nell.utils.UsageCountryUtil

/** 额度确认页 */
@SuppressLint("CustomSplashScreen")
class LoanConfirmActivity : BaseActivity() {

    //布局binding
    private lateinit var mActivityBinding: ActivityFirstNellLoanConfirmBinding

    //贷款viewModel
    private val mLoanViewModel by lazy {
        ViewModelProvider(this)[LoanViewModel::class.java]
    }

    //用户流程viewModel
    private val mUserStateViewModel by lazy {
        ViewModelProvider(this)[UserStateViewModel::class.java]
    }

    override fun onFirstNellBindingFirstNellViewFirstNellModel(): List<ViewModel> {
        val list = mutableListOf<ViewModel>()
        list.add(mLoanViewModel)
        list.add(mUserStateViewModel)
        return list
    }

    override fun onFirstNellBindingFirstNellViewFirstNellTag(): String {
        return PageTagEnum.apply.name.uppercase()
    }

    override fun onFirstNellBindingFirstNellContentFirstNellView(): View {
        mActivityBinding = ActivityFirstNellLoanConfirmBinding.inflate(layoutInflater)
        return mActivityBinding.root
    }

    @SuppressLint("SetTextI18n")
    override fun onFirstNellContentFirstNellViewFirstNellBinded() {
        try {
            //获取用户状态回调
            mUserStateViewModel.onFirstNellFetchFirstNellUserFirstNellStateFirstNellLiveData.observe(
                this
            ) {
                if (it.isSuccess) {
                    //判断所有步骤是否完成
                    val isFinishAllExtSteps =
                        InfoProcessUtil.isFirstNellFinishFirstNellAllFirstNellPageFirstNellConfigFirstNellAfterFirstNellApplyFirstNellPage()
                    if (isFinishAllExtSteps) {
                        //创建贷款
                        mLoanViewModel.createFirstNellLoan(ReqBase())
                    } else {
                        startFirstNellToFirstNellNextFirstNellFromFirstNellPage(PageTagEnum.apply)
                    }
                }
            }

            //获取借款信息回调
            mLoanViewModel.onFirstNellFetchFirstNellLoanFirstNellConfirmFirstNellDetailFirstNellLiveData.observe(
                this
            ) {
                if (it.isSuccess) {
                    it.getOrNull()?.let { result ->

                        mActivityBinding.textLoanConfirmAmountValue.text =
                            (result.currency+ " " + result.displayFirstNellAmount()
                                .toFirstNellFinanceFirstNellStringFirstNellWithFirstNellMoneyFirstNellFlag())

                        mActivityBinding.textLoanConfirmTermValue.text =
                            (result.displayFirstNellDays())

                        //到账信息
                        mActivityBinding.textLoanConfirmDisburseAmountValue.text =
                            result.currency + " " + result.displayFirstNellRealFirstNellAmount()

                        mActivityBinding.textLoanConfirmDisburseLoanAmountValue.text =
                            result.currency+ " " + result.displayFirstNellAmount()

                        mActivityBinding.textLoanConfirmDisburseServiceFeeValue.text =
                            result.currency+ " " + result.displayFirstNellProcessingFirstNellFee()

                        mActivityBinding.textLoanConfirmDisburseServiceFeeVatValue.text =
                            result.currency+ " " + result.displayFirstNellProcessingFirstNellVatFirstNellFee()

                        //还款信息
                        mActivityBinding.textLoanConfirmRepayAmountValue.text =
                            result.currency+ " " + result.displayFirstNellRepayFirstNellAmount()

                        mActivityBinding.textLoanConfirmRepayLoanAmountValue.text =
                            result.currency+ " " + result.displayFirstNellAmount()

                        mActivityBinding.textLoanConfirmRepayInterestValue.text =
                            result.currency+ " " + result.displayFirstNellInterest()

                        mActivityBinding.textLoanConfirmRepayServiceFeeValue.text =
                            result.currency+ " " + result.displayFirstNellRepayFirstNellProFirstNellFee()

                        mActivityBinding.textLoanConfirmRepayServiceFeeVatValue.text =
                            result.currency+ " " + result.displayFirstNellRepayFirstNellProFirstNellVatFirstNellFee()

                        mActivityBinding.textLoanConfirmRepayOverduePenaltyInterestValue.text =
                            result.displayFirstNellOverdueFirstNellRate()
                                .toFirstNellInterestFirstNellRateFirstNellString()
                    }
                }
            }

            //确认借款回调
            mLoanViewModel.onFirstNellCreateFirstNellLoanFirstNellLiveData.observe(this) {
                if (it.isSuccess) {
                    //直接转到审核中
                    startFirstNellToFirstNellLoanFirstNellReviewFirstNellActivity()
                }
            }

            //点击确认借款
            mActivityBinding.submitLoanConfirm.setOnClickListener {
                //记录埋点
                mActivityBinding.submitLoanConfirm.recordFirstNellEvent(
                    type = mActivityBinding.submitLoanConfirm.getFirstNellScenFirstNellClickFirstNellFlag(
                        onFirstNellBindingFirstNellViewFirstNellTag()
                    ),
                    remark = ""
                )

                //查询用户状态
                mUserStateViewModel.fetchFirstNellUserFirstNellState()
            }

            //如果为科特，需要显示vat字段
            mActivityBinding.rlLoanConfirmDisburseServiceFeeVat.visibility = View.GONE
            mActivityBinding.rlLoanConfirmRepayServiceFeeVat.visibility = View.GONE
            if (UsageCountryUtil.isFirstNellCountryFirstNellCotediva()) {
                mActivityBinding.rlLoanConfirmDisburseServiceFeeVat.visibility =
                    View.VISIBLE
                mActivityBinding.rlLoanConfirmRepayServiceFeeVat.visibility =
                    View.VISIBLE
            }

            //获取借款信息
            mLoanViewModel.fetchFirstNellLoanFirstNellConfirmFirstNellDetail(
                ReqBase()
            )

        } catch (e: Exception) {
            e.printStackTrace()
        }
    }

    override fun isFirstNellAllFirstNellFormFirstNellDataFirstNellCompleted(): String {
        val notFillOptions = mutableListOf<String>()

        if (notFillOptions.isEmpty()) {
            return ""
        }
        notFillOptions.add(0, "NOT_FILL")
        return notFillOptions.joinToString("#")
    }

}